/**
 * Created by Administrator on 2017/9/23.
 */
$(document).ready(function(){

    var img_url = "http://47.92.37.168/supermarket/img/";
    var username = sessionStorage.getItem('username');//缓存 获取用户名
    var num = sessionStorage.getItem('num');//缓存 获取选项 index值
    console.log(num);
    var start = 0;//开始获取的数据
    var classify = null;//选项种类
    var flag_update = true;//默认状态下向后台请求发送请求
    map(num);
    function map(num) {
        switch (num) {
            case '0':
                classify = 'muying';
                break;
            case '1':
                classify = 'lingshi';
                break;
            case '2':
                classify = 'jiushui';
                break;
            case '3':
                classify = 'liangyou';
                break;
            case '4':
                classify = 'meirong';
                break;
            case '5':
                classify = 'jingxuan';
                break;
            case '6':
                classify = 'jiaqing';
                break;
            case '7':
                classify = 'jiayong';
                break;
            case '8':
                classify = 'shengxian';
                break;
            case '9':
                classify = 'jiayong';
                break;
        }
    }
    getData(start,classify);
    function getData(start, classify) {
        $.ajax({
            type: "post",
            url: "http://47.92.37.168/supermarket/data/get_commodity.php",
            async: true,
            data: {
                'start': start,
                'classify': classify
            },
            // dataType:"jsonp",
            success: function (data) {
                console.log(data);
                if (data) {
                    for (var i=0;i< data.length;i++) {
                        $('.swiper2 .swiper-slide').eq(num).append('<div class="slide-2" id=' + data[i].id + ' >' +
                            '<a href="javascript:;">' +
                            '<img src='+img_url + data[i].img+' class="list_img">' +
                            '<div class="jieshao">' + data[i].name + '</div>' +
                            '<div class="xiaoliang">' +
                            '<p class="xiao-1">' +
                            '<span class="zongjian">月销量' + data[i].count + '件</span>' +
                            '<span class="danjia">' + data[i].price + '</span>' +
                            '</p>' +
                            '<p class="xiao-2"><i class="fa fa-shopping-cart"></i></p>' +
                            '</div>' +
                            '</a>' +
                            '</div>')
                    }
                    if (data.length < 4) {
                        flag_update = false;
                    }
                } else {
                    flag_update = false;
                }
            }
        });
    }
    // tab切换
    var swiper1 = new Swiper('.swiper1', {
        freeMode: true,
        freeModeMomentumRatio: 0.5,
        slidesPerView: 'auto',
        onTap: function () {
            start=0;
            flag_update=true;

            var btnIndex = swiper1.clickedIndex;
            $('.swiper1 .swiper-slide').eq(btnIndex).addClass('active1').siblings().removeClass('active1');
            swiper2.slideTo(btnIndex);
            num=btnIndex;
            map(num+'');
            $('.swiper2 .swiper-slide').empty();
            getData(start, classify);
        }
    });
// tab内容切换
    var swiper2 = new Swiper('.swiper2', {
        slidesPerView: 1,
        onSlideChangeStart: function () {
            var actIndex = swiper2.activeIndex;
            flag_update=true;
            start = 0;
            num = actIndex;
            $('.swiper1 .swiper-slide').eq(actIndex).addClass('active1').siblings().removeClass('active1');
            swiper1.slideTo(actIndex);
            map(num+'');
            $('.swiper2 .swiper-slide').empty();
            getData(start, classify);
        }
    });
    //向上滑动更新数据
    $(window).on('touchend', function (e) {
        stopPro(e);
        var _this = $(this);
        if ($(this).scrollTop() == $(this).height() - $(window).height() && flag_update) {
            start += 4;
            getData(start, classify);
        }
    });
    //点击商品购物车按钮 把产品加入购物车
    $(document).on('click', '.xiao-2', function (e) {
        stopPro(e);
        var _this = $(this);
        if (username) {
            $.ajax({
                type: "get",
                url: "http://47.92.37.168/supermarket/data/my_commodity_car.php",
                async: true,
                data: {
                    'user_phone': username,
                    'commodity_id': $(this).parents('.slide-2').attr('id'),
                    'count': 1
                },
                dataType: "jsonp",
                jsonp: "callback",
                success: function (data) {
                    console.log(data);
                    if (data.msg == 'success') {
                        //图片移动效果
                        var left = $('.carta').offset().left;//距离文档左侧的距离
                        var top = $('.carta').offset().top;//距离文档顶部的距离
                        $('body').append("<img class='addImg' src='" + _this.parents('a').find('.list_img').attr('src') + "' />");
                        $('.addImg').css({'left': _this.offset().left + 'px', 'top': _this.offset().top + 'px'});
                        $('.addImg').animate({'left': left + 'px', 'top': top + 'px'}, 1000, function () {
                            $('.addImg').remove();
                        })
                    }
                }
            })
        } else {
            location.href = "login.html";
        }
    });
    //点击顶部购物车按钮，跳到购物车页面
    $('.carta').click(function(){
        if (username) {
            location.href="shopping.html";
        } else{
            location.href="login.html";
        }
    });
});
//取消默认事件  兼容
    function preDef(evt) {
        var e = evt || window.event;
        if (e.preventDefault) {
            e.preventDefault();
        } else {
            e.returnValue = false;
        }
    }

//阻止冒泡的函数
    function stopPro(evt) {
        var e = evt || window.event;
        if (e.stopPropagation) {
            e.stopPropagation();//w3c阻止冒泡方法
        } else {
            e.cancelBubble = true;//IE阻止冒泡方法
        }
    };